{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '，' (U+FF0C) (3273904882.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[1], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    要列出指定文件夹下所有内容相同的文件名，可以使用Python编程语言。以下是一个示例代码，它使用`hashlib`库来计算文件的哈希值，并比较这些哈希值以找出内容相同的文件。\u001b[0m\n\u001b[1;37m                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '，' (U+FF0C)\n"
     ]
    }
   ],
   "source": [
    "要列出指定文件夹下所有内容相同的文件名，可以使用Python编程语言。以下是一个示例代码，它使用`hashlib`库来计算文件的哈希值，并比较这些哈希值以找出内容相同的文件。\n",
    "\n",
    "```python\n",
    "import os\n",
    "import hashlib\n",
    "\n",
    "def calculate_file_hash(file_path):\n",
    "    \"\"\"计算文件的哈希值\"\"\"\n",
    "    hash_func = hashlib.md5()\n",
    "    with open(file_path, 'rb') as f:\n",
    "        for chunk in iter(lambda: f.read(4096), b\"\"):\n",
    "            hash_func.update(chunk)\n",
    "    return hash_func.hexdigest()\n",
    "\n",
    "def find_duplicate_files(folder_path):\n",
    "    \"\"\"找出指定文件夹下内容相同的文件\"\"\"\n",
    "    file_hash_map = {}\n",
    "    for root, dirs, files in os.walk(folder_path):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            file_hash = calculate_file_hash(file_path)\n",
    "            if file_hash in file_hash_map:\n",
    "                file_hash_map[file_hash].append(file_path)\n",
    "            else:\n",
    "                file_hash_map[file_hash] = [file_path]\n",
    "\n",
    "    duplicate_files = {hash_val: files for hash_val, files in file_hash_map.items() if len(files) > 1}\n",
    "    return duplicate_files\n",
    "\n",
    "# 指定文件夹路径\n",
    "folder_path = '/path/to/your/folder'\n",
    "\n",
    "# 找出内容相同的文件\n",
    "duplicate_files = find_duplicate_files(folder_path)\n",
    "\n",
    "# 打印结果\n",
    "for hash_val, files in duplicate_files.items():\n",
    "    print(f\"内容相同的文件组（哈希值: {hash_val}):\")\n",
    "    for file in files:\n",
    "        print(file)\n",
    "```\n",
    "\n",
    "请将`/path/to/your/folder`替换为你想要检查的文件夹的实际路径。\n",
    "\n",
    "这段代码的工作原理如下：\n",
    "\n",
    "1. `calculate_file_hash`函数计算文件的MD5哈希值。\n",
    "2. `find_duplicate_files`函数遍历指定文件夹及其子文件夹中的所有文件，计算每个文件的哈希值，并将文件路径存储在哈希值对应的列表中。\n",
    "3. 最后，代码打印出所有内容相同的文件组，每个组包含相同哈希值的文件路径。\n",
    "\n",
    "请注意，MD5哈希值可能不是绝对唯一的，但在大多数情况下，它可以有效地检测出内容相同的文件。如果你需要更高的准确性，可以考虑使用其他哈希算法，如SHA-256。."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import hashlib\n",
    "\n",
    "def calculate_file_hash(file_path):\n",
    "    \"\"\"计算文件的哈希值\"\"\"\n",
    "    hash_func = hashlib.md5()\n",
    "    with open(file_path, 'rb') as f:\n",
    "        for chunk in iter(lambda: f.read(4096), b\"\"):\n",
    "            hash_func.update(chunk)\n",
    "    return hash_func.hexdigest()\n",
    "\n",
    "def find_duplicate_files(folder_path):\n",
    "    \"\"\"找出指定文件夹下内容相同的文件\"\"\"\n",
    "    file_hash_map = {}\n",
    "    for root, dirs, files in os.walk(folder_path):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            file_hash = calculate_file_hash(file_path)\n",
    "            if file_hash in file_hash_map:\n",
    "                file_hash_map[file_hash].append(file_path)\n",
    "            else:\n",
    "                file_hash_map[file_hash] = [file_path]\n",
    "\n",
    "    duplicate_files = {hash_val: files for hash_val, files in file_hash_map.items() if len(files) > 1}\n",
    "    return duplicate_files\n",
    "\n",
    "# 指定文件夹路径\n",
    "folder_path = '/path/to/your/folder'\n",
    "\n",
    "# 找出内容相同的文件\n",
    "duplicate_files = find_duplicate_files(folder_path)\n",
    "\n",
    "# 打印结果\n",
    "for hash_val, files in duplicate_files.items():\n",
    "    print(f\"内容相同的文件组（哈希值: {hash_val}):\")\n",
    "    for file in files:\n",
    "        print(file)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "作业7马蔚\n",
    "CodeGeex生成代码\n",
    "生成一段代码，列出指定文件夹下所有内容相同的文件名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import hashlib\n",
    "\n",
    "def calculate_file_hash(file_path):\n",
    "    \"\"\"计算文件的哈希值\"\"\"\n",
    "    hash_func = hashlib.md5()\n",
    "    with open(file_path, 'rb') as f:\n",
    "        for chunk in iter(lambda: f.read(4096), b\"\"):\n",
    "            hash_func.update(chunk)\n",
    "    return hash_func.hexdigest()\n",
    "\n",
    "def find_duplicate_files(folder_path):\n",
    "    \"\"\"找出指定文件夹下内容相同的文件\"\"\"\n",
    "    file_hash_map = {}\n",
    "    for root, dirs, files in os.walk(folder_path):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            file_hash = calculate_file_hash(file_path)\n",
    "            if file_hash in file_hash_map:\n",
    "                file_hash_map[file_hash].append(file_path)\n",
    "            else:\n",
    "                file_hash_map[file_hash] = [file_path]\n",
    "\n",
    "    duplicate_files = {hash_val: files for hash_val, files in file_hash_map.items() if len(files) > 1}\n",
    "    return duplicate_files\n",
    "\n",
    "# 指定文件夹路径\n",
    "folder_path = '/path/to/your/folder'\n",
    "\n",
    "# 找出内容相同的文件\n",
    "duplicate_files = find_duplicate_files(folder_path)\n",
    "\n",
    "# 打印结果\n",
    "for hash_val, files in duplicate_files.items():\n",
    "    print(f\"内容相同的文件组（哈希值: {hash_val}):\")\n",
    "    for file in files:\n",
    "        print(file)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
